$().ready(function() {
	validateRule();
    select();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
//分页下拉框
function select() {
    $('#selectPage').selectPage({
        showField : 'mobile',
        keyField : 'id',
        data :'/sys/user/list',
        searchField:'name',
        //格式化显示项目，提供源数据进行使用
        formatItem : function(data){
            return data.name + '(' + data.mobile + ')';
        },
        eAjaxSuccess : function(d){
            var result;
            if(d) result = d.data;
            else result = undefined;
            return result;
        }
    });
}

//分页下拉框
$("#profileId").change(function () {
    $.ajax({
        url: '/jzweb/itemBank/byKnowledges',
        data :{cprofileId:$(this).val()} ,
        success: function (r) {

            var list ='<select id="kpointsId" name="kpointsId" class="form-control">';
            if(r.data.length==0){
                list+='<option value="">暂无数据</option>';
            }else{
                list+='<option value="">请选择知识点领域</option>';
                for(var p in r.data){
                    list+='<option value="'+r.data[p].id+'">'+r.data[p].name+'</option>';
                }
            }
            list+='</select>';
            $("#kpointsIdDiv").html(list);
        }
    })
})

function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/experts/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
            userId : {
				required : true
			},
            kpointsId : {
                required : true
            },
            profileId : {
                required : true
            },
            orgId : {
                required : true
            },
            type : {
                required : true
            },
            enclosure : {
                required : true
            }
		},
		messages : {
			name : {
				required : icon + "请选择会员"
			},
            kpointsId : {
                required : icon + "请选择知识点领域"
            },
            profileId : {
                required : icon + "请选择负责证书"
            },
            orgId : {
                required : icon + "请输入所属机构"
            },
            type : {
                required : icon + "请选择专家类型"
            },
            enclosure : {
                required : icon + "请上传文件"
            }
		}
	})
}

layui.use('upload', function () {
    var upload = layui.upload;
    //执行实例
    upload.render({
        elem: '#btnUpload', //绑定元素
        url: '/common/sysFile/upload', //上传接口
        accept: 'file',
        done: function (r) {
            $("#enclosure").val(r.data);
            $("#test").html(subFileName(r.data));
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});